package com.ahnchan.ContactsPlus;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.SeekBar;

public class ColorDialog extends AlertDialog {

	public static String CONTACTS = "CONTACTS";
	public static String GROUP = "GROUP";
	public static String COMPANY = "COMPANY";
	public static String FAVORITES = "FAVORITTES";
	
	private ContactsPlusSetting setting;	// Properties
	
	private ListActivity mActivity;
	private String mClassName;
	
	private View mView;
	
	private SeekBar mIndexBGBar;
	private View mIndexBGPreview;
	private CheckBox mCkIndexBG;
	
	private View mIndexFontPreview;
	private SeekBar mIndexFontBar;
	private CheckBox mCkIndexFont;
	
	private View mFontBGPreview;
	private SeekBar mFontBGBar;
	private CheckBox mCkFontBG;
	
	private View mFontPreview;
	private SeekBar mFontBar;
	private CheckBox mCkFont;
	
//	static private float mMaxValue = 63;

	protected ColorDialog(Context context, ListActivity activity, String className) {
		super(context);
		mActivity = activity;
		mClassName  = className;
		
		setting = ContactsPlusSetting.instatnce();
		
		mView = LayoutInflater.from(context).inflate(R.layout.color_picker, null);
		setView(mView);
		
		//----------------------
		// Index Background color
		//----------------------
		mIndexBGBar = (SeekBar)mView.findViewById(R.id.color_index_bg);
		mIndexBGBar.setMax(128);
		float[] indexBGHSV = new float[3];    		
		Color.colorToHSV(setting.getIndexTitleBGColor(), indexBGHSV);
		int indexBGH = (int) (indexBGHSV[0] * mIndexBGBar.getMax() / 360);
		mIndexBGBar.setProgress(indexBGH);
		mIndexBGBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			public void onStopTrackingTouch(SeekBar seekBar) {
				// nothing
			}
			public void onStartTrackingTouch(SeekBar seekBar) {
				// nothing
			}
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// Change Dialog text (Font Size)
				float[] hsv = new float[3];
				
				if (mCkIndexBG.isChecked()) {
					hsv[0] = 0;
					hsv[1] = 0;
					hsv[2] = seekBar.getProgress() / (float) 63;					
				} else {
					hsv[0] = 360 * seekBar.getProgress() / (float) seekBar.getMax();
					hsv[1] = 63;
					hsv[2] = 63;
				}
				int iColor = Color.HSVToColor(hsv);
	    		mIndexBGPreview.setBackgroundColor(iColor);
				
				// Change index background color
	    		if (CONTACTS.equals(mClassName)) {
	    			((ContactsActivity)mActivity).changeIndexFontBGColor(iColor);
	    		} else if (GROUP.equals(mClassName)) {
	    			((GroupActivity)mActivity).changeIndexFontBGColor(iColor);
	    		} else if (COMPANY.equals(mClassName)) {
	    			((CompanyActivity)mActivity).changeIndexFontBGColor(iColor);
	    		} else if (FAVORITES.equals(mClassName)) {
	    			((FavoritesActivity)mActivity).changeIndexFontBGColor(iColor);
	    		}
			}
		});
		
		mCkIndexBG = (CheckBox) mView.findViewById(R.id.check_color_index_bg);
		mCkIndexBG.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {            	
            	if (((CheckBox)v).isChecked()) {
            		mIndexFontBar.setMax(63);
            	} else {
            		mIndexFontBar.setMax(128);
            	}
            	
            	mIndexFontPreview.setBackgroundColor(setting.getIndexTitleColor());
            	
            	float[] indexFontHSV = new float[3];    		
        		Color.colorToHSV(setting.getIndexTitleColor(), indexFontHSV);
            	int indexFontH = (int) (indexFontHSV[0] * 63);
        		mIndexFontBar.setProgress(indexFontH);
            }
        });


		mIndexBGPreview = mView.findViewById(R.id.preview_index_bg);
		mIndexBGPreview.setBackgroundColor(setting.getIndexTitleBGColor());
	
		//----------------------
		// Index Font color
		//----------------------
		mIndexFontBar = (SeekBar)mView.findViewById(R.id.color_index_font);
		mIndexFontBar.setMax(128);
		float[] indexFontHSV = new float[3];    		
		Color.colorToHSV(setting.getIndexTitleColor(), indexFontHSV);
		int indexFontH = (int) (indexFontHSV[0] * mIndexFontBar.getMax() / 360);
		mIndexFontBar.setProgress(indexFontH);
		mIndexFontBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			public void onStopTrackingTouch(SeekBar seekBar) {
				// nothing
			}
			public void onStartTrackingTouch(SeekBar seekBar) {
				// nothing
			}
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// Change Dialog text (Font Size)
				float[] hsv = new float[3];
				
				if (mCkIndexFont.isChecked()) {
					hsv[0] = 0;
					hsv[1] = 0;
					hsv[2] = seekBar.getProgress() / (float) 63;					
				} else {
					hsv[0] = 360 * seekBar.getProgress() / (float) seekBar.getMax();
					hsv[1] = 63;
					hsv[2] = 63;
				}
				int iColor = Color.HSVToColor(hsv);

				mIndexFontPreview.setBackgroundColor(iColor);
				
				// Change index background color
	    		if (CONTACTS.equals(mClassName)) {
	    			((ContactsActivity)mActivity).changeIndexFontColor(iColor);
	    		} else if (GROUP.equals(mClassName)) {
	    			((GroupActivity)mActivity).changeIndexFontColor(iColor);
	    		} else if (COMPANY.equals(mClassName)) {
	    			((CompanyActivity)mActivity).changeIndexFontColor(iColor);
	    		} else if (FAVORITES.equals(mClassName)) {
	    			((FavoritesActivity)mActivity).changeIndexFontColor(iColor);
	    		}
//	    		((ContactsActivity)mActivity).changeIndexFontColor(iColor);
			}
		});
		
		mCkIndexFont = (CheckBox) mView.findViewById(R.id.check_color_index_font);
		mCkIndexFont.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {            	
            	if (((CheckBox)v).isChecked()) {
            		mIndexFontBar.setMax(63);
            	} else {
            		mIndexFontBar.setMax(128);
            	}
            	
            	mIndexFontPreview.setBackgroundColor(setting.getIndexTitleColor());
            	
            	float[] indexFontHSV = new float[3];    		
        		Color.colorToHSV(setting.getIndexTitleColor(), indexFontHSV);
            	int indexFontH = (int) (indexFontHSV[0] * 63);
        		mIndexFontBar.setProgress(indexFontH);
            }
        });
		
		mIndexFontPreview = mView.findViewById(R.id.preview_index_font);
		mIndexFontPreview.setBackgroundColor(setting.getIndexTitleColor());
		
		//----------------------
		// Font background color
		//----------------------
		mFontBGBar = (SeekBar)mView.findViewById(R.id.color_font_bg);
		mFontBGBar.setMax(128);
		float[] fontBGHSV = new float[3];    		
		Color.colorToHSV(setting.getIndexTitleColor(), fontBGHSV);
		int fontBGH = (int) (fontBGHSV[0] * mFontBGBar.getMax() / 360);
		mFontBGBar.setProgress(fontBGH);
		mFontBGBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			public void onStopTrackingTouch(SeekBar seekBar) {
				// nothing
			}
			public void onStartTrackingTouch(SeekBar seekBar) {
				// nothing
			}
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// Change Dialog text (Font Size)
				float[] hsv = new float[3];
				
				if (mCkFontBG.isChecked()) {
					hsv[0] = 0;
					hsv[1] = 0;
					hsv[2] = seekBar.getProgress() / (float) 63;					
				} else {
					hsv[0] = 360 * seekBar.getProgress() / (float) seekBar.getMax();
					hsv[1] = 63;
					hsv[2] = 63;
				}
				int iColor = Color.HSVToColor(hsv);

				mFontBGPreview.setBackgroundColor(iColor);
				
				// Change index background color
	    		if (CONTACTS.equals(mClassName)) {
	    			((ContactsActivity)mActivity).changeFontBGColor(iColor);
	    		} else if (GROUP.equals(mClassName)) {
	    			((GroupActivity)mActivity).changeFontBGColor(iColor);
	    		} else if (COMPANY.equals(mClassName)) {
	    			((CompanyActivity)mActivity).changeFontBGColor(iColor);
	    		} else if (FAVORITES.equals(mClassName)) {
	    			((FavoritesActivity)mActivity).changeFontBGColor(iColor);
	    		}

//	    		((ContactsActivity)mActivity).changeFontBGColor(iColor);
			}
		});
		
		mCkFontBG = (CheckBox) mView.findViewById(R.id.check_color_font_bg);
		mCkFontBG.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {            	
            	if (((CheckBox)v).isChecked()) {
            		mFontBGBar.setMax(63);
            	} else {
            		mFontBGBar.setMax(128);
            	}
            	
            	mFontBGPreview.setBackgroundColor(setting.getFontBGColor());
            	
            	float[] fontBGHSV = new float[3];    		
        		Color.colorToHSV(setting.getFontBGColor(), fontBGHSV);
            	int fontBGH = (int) (fontBGHSV[0] * 63);
        		mFontBGBar.setProgress(fontBGH);
            }
        });
		
		mFontBGPreview = mView.findViewById(R.id.preview_font_bg);
		mFontBGPreview.setBackgroundColor(setting.getFontBGColor());
		
		//----------------------
		// Font color
		//----------------------
		mFontBar = (SeekBar)mView.findViewById(R.id.color_font);
		mFontBar.setMax(128);
		float[] fontHSV = new float[3];    		
		Color.colorToHSV(setting.getFontColor(), fontHSV);
		int fontH = (int) (fontHSV[0] * mFontBar.getMax() / 360);
		mFontBar.setProgress(fontH);
		mFontBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			public void onStopTrackingTouch(SeekBar seekBar) {
				// nothing
			}
			public void onStartTrackingTouch(SeekBar seekBar) {
				// nothing
			}
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// Change Dialog text (Font Size)
				float[] hsv = new float[3];
				
				if (mCkFont.isChecked()) {
					hsv[0] = 0;
					hsv[1] = 0;
					hsv[2] = seekBar.getProgress() / (float) 63;					
				} else {
					hsv[0] = 360 * seekBar.getProgress() / (float) seekBar.getMax();
					hsv[1] = 63;
					hsv[2] = 63;
				}
				int iColor = Color.HSVToColor(hsv);

				mFontPreview.setBackgroundColor(iColor);
				
				// Change index background color
	    		if (CONTACTS.equals(mClassName)) {
	    			((ContactsActivity)mActivity).changeFontColor(iColor);
	    		} else if (GROUP.equals(mClassName)) {
	    			((GroupActivity)mActivity).changeFontColor(iColor);
	    		} else if (COMPANY.equals(mClassName)) {
	    			((CompanyActivity)mActivity).changeFontColor(iColor);
	    		} else if (FAVORITES.equals(mClassName)) {
	    			((FavoritesActivity)mActivity).changeFontColor(iColor);
	    		}

//	    		((ContactsActivity)mActivity).changeFontColor(iColor);
			}
		});
		
		mCkFont = (CheckBox) mView.findViewById(R.id.check_color_font);
		mCkFont.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {            	
            	if (((CheckBox)v).isChecked()) {
            		mFontBar.setMax(63);
            	} else {
            		mFontBar.setMax(128);
            	}
            	
            	mFontPreview.setBackgroundColor(setting.getFontColor());
            	
            	float[] fontHSV = new float[3];    		
        		Color.colorToHSV(setting.getFontColor(), fontHSV);
            	int fontH = (int) (fontHSV[0] * 63);
        		mFontBar.setProgress(fontH);
            }
        });
		
		mFontPreview = mView.findViewById(R.id.preview_font);
		mFontPreview.setBackgroundColor(setting.getFontColor());
	}

}
